﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Kinect_Interface_Prototype_1.Kinect_MediaPlayer
{
    public class Library
    {       
        public String LibraryPath { get; set; }
        public List<Artist> Artists { get; set; }
        private MediaPlayerForm Form { get; set; }

        public Library(MediaPlayerForm form, string libraryPath)
        {
            Form = form;
            LibraryPath = libraryPath;
            Artists = new List<Artist>();
        }

        public void BuildLibrary()
        {
            List<string> foldersInFolder;

            try
            {
                foldersInFolder = Directory.GetDirectories(LibraryPath).ToList();

                if (foldersInFolder.Count <= 0)
                {
                    Form.noLibraryLabelSetText("No folders in specified path.");
                    Form.noLibraryPanelShow(true);
                    return;
                }
                else
                {
                    Form.noLibraryPanelShow(false);
                }

                foreach (var folderPath in foldersInFolder)
                {
                    Artist artist = new Artist();

                    List<string> innerFolders = Directory.GetDirectories(folderPath).ToList();

                    List<Album> Albums = innerFolders.Select(x => new Album(x)).ToList();

                    foreach (var album in Albums)
                    {
                        List<string> filesInFolder = Directory.GetFiles(album.AlbumFolderPath).ToList();

                        // Set artist name
                        if (artist.ArtistName == "Unknown")
                        {
                            string songFile = filesInFolder.Where(x => MediaPlayerForm.acceptedFileTypes.Any(a => x.Contains(a))).FirstOrDefault();

                            if (!String.IsNullOrEmpty(songFile))
                            {
                                Song song = new Song(songFile);
                                if (song != null && !String.IsNullOrEmpty(song.Artist))
                                    artist.ArtistName = song.Artist;
                            }
                        }

                        foreach (string songPath in filesInFolder.Where(x => MediaPlayerForm.acceptedFileTypes.Any(a => x.Contains(a))))
                        {
                            Song song = new Song(songPath);

                            // Set album title
                            if (!String.IsNullOrEmpty(song.Album))
                            {
                                album.Title = song.Album;
                                album.Year = song.Year;
                            }
                        
                            album.Songs.Add(song);
                        }

                        // if their are no songs in the folder, continue and don't add
                        if (!album.Songs.Any())
                            continue;

                        album.Songs = album.Songs.OrderBy(x => x.Track).ToList();

                        filesInFolder = filesInFolder.Select(x => x.Substring(x.LastIndexOf("\\"), x.Length - x.LastIndexOf("\\"))).ToList();

                        string artPath = filesInFolder.Where(x => x.Contains(".jpg") && x.Contains(album.Title)).FirstOrDefault()
                                         ?? filesInFolder.Where(x => x.Contains(".jpg") && x.Contains("Large")).FirstOrDefault()
                                         ?? filesInFolder.Where(x => x.Contains(".jpg")).FirstOrDefault();

                        album.AlbumArtPath = artPath != null ? album.AlbumFolderPath + artPath : Constants.MediaPlayerImagesFolder + "unknownAlbum.jpg";


                        artist.Albums.Add(album);
                    }

                    // if there are no albums in the folder, continue and don't add artist
                    if (!artist.Albums.Any())
                        continue;

                    Artists.Add(artist);
                }

                if (!Artists.Any())
                {
                    Form.noLibraryLabelSetText("No artist or albums at the library path.");
                    Form.noLibraryPanelShow(true);
                }
            }
            catch (Exception e)
            {
                if (e is ArgumentException || e is DirectoryNotFoundException)
                {
                    Form.noLibraryLabelSetText("Invalid library path.");
                    Form.noLibraryPanelShow(true);
                    return;
                }
                else
                {
                    throw e;
                }
            }
        }
    }
}
